package lc;
import java.util.*;
import org.junit.*;

public class Ex12 {
    class Solution {
        public String intToRoman(int num) {
            Map<Integer, String[]> map = new HashMap<>(){{
                put(1, new String[]{"I", "IV", "V", "IX"});
                put(2, new String[]{"X", "XL", "L", "XC"});
                put(3, new String[]{"C", "CD", "D", "CM"});
            }};
            int cur = num;
            int mod = 10;
            int d = 1;
            StringBuilder sb = new StringBuilder();
            while (cur > 0) {
                int t = cur % mod;
                if (d <= 3) {
                    String[] strs = map.get(d);
                    if (t == 0) {

                    } else if (t < 4) {
                        for (int i = 0; i < t; i++) {
                            sb.insert(0, strs[0]);
                        }
                    } else if(t == 4) {
                        sb.insert(0, strs[1]);
                    } else if(t == 9) {
                        sb.insert(0, strs[3]);
                    } else {
                        for (int i = 0; i < (t - 5); i++) {
                            sb.insert(0, strs[0]);
                        }
                        sb.insert(0, strs[2]);
                    }
                } else {
                    for (int i = 0; i < t; i++) {
                        sb.insert(0, "M");
                    }
                }
                d++;
                cur /= 10;
            }

            return sb.toString();
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        System.out.println(s.intToRoman(1994));
    }
}
